//
//  SearchForDiseaseForIphoneVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/6.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "SearchForDiseaseForIphoneVC.h"
#import "SearchForDiseaseListIPhoneCell.h"
#import "SearchForDiseaseDetailVC.h"
#define  ListCellID @"cellid1"
@interface SearchForDiseaseForIphoneVC ()

@end

@implementation SearchForDiseaseForIphoneVC
#pragma mark - Life cycle
- (void)viewWillAppear:(BOOL)animated{
    self.page = 1;
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self setWebData];
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetNav];
}
#pragma mark - Property

#pragma mark - UI
- (void)resetNav{
    [self setTheBackBtn];
    [self setRightBtnImg:[UIImage imageNamed:@"add_symbol"]];
    self.myTitle.text = @"病害巡查";
    self.pullRefreshEnable = YES;
    self.pullLoadMoreEnable = YES;
    [self.tableView registerClass:[SearchForDiseaseListIPhoneCell class] forCellReuseIdentifier:ListCellID];
}
#pragma mark - UI Response
- (void)rightButtonClick{
    SearchForDiseaseDetailVC *con = [[SearchForDiseaseDetailVC alloc]init];
    con.state = 0;
    [self.navigationController pushViewController:con animated:YES];
}
#pragma mark - Data handle
- (void)setWebData{
    [NetWorkTool getSearchForDiseaseWithPage:self.page pageSize:10 block:^(id data) {
       [self performSelector:@selector(MJRefreshStop) withObject:nil afterDelay:1];
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                if (self.page == 1) {
                    self.tableView.contentOffset = CGPointMake(0, 0);
                    [self.dataArray removeAllObjects];
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }else{
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }
                if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                    if ([data[@"data"][@"items"] count]<10&&(self.dataArray.count!=0)) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                if (self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                }else{
                    self.noDataView.hidden = YES;
                }
                [self.tableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                   [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
                
            }
           
        }else{
           [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES
         ];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchForDiseaseListIPhoneCell *cell = (SearchForDiseaseListIPhoneCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchForDiseaseDetailVC *con = [[SearchForDiseaseDetailVC alloc]init];
    NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"vcBooleanCode"]];
    if ([str integerValue]==1) {
        con.state = 2;
    }else{
        con.state = 1;
    }
    con.codeId = self.dataArray[indexPath.row][@"diseaseInspectionId"];
   [self.navigationController pushViewController:con animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchForDiseaseListIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
    [cell searchForDiseaseAddValue:self.dataArray[indexPath.row]];
    return  cell;
}
@end
